//
//  SearchNearPointViewController.m
//  AppointmentForCar
//
//  Created by ZengPengYuan on 2016/11/14.
//  Copyright © 2016年 Herron. All rights reserved.
//

#import "SearchNearPointViewController.h"
#import "SearchNearPointViewModel.h"

@interface SearchNearPointViewController ()

@property (nonatomic , strong) UIView *searchView;
@property (nonatomic , weak) UITableView *resultTableView;
@property (nonatomic , strong) SearchNearPointViewModel *model;

@end

@implementation SearchNearPointViewController

#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    // 搜索结果点击事件回调
    __weak __typeof__(self) weakSelf = self;
    [self.model searchClickItem:^(BMKPoiInfo *info) {
        __strong __typeof__(weakSelf) strongSelf = weakSelf;
        if([strongSelf.delegate respondsToSelector:@selector(searchNearPointWithResult:)]) {
            [strongSelf.delegate searchNearPointWithResult:info];
            [strongSelf.navigationController popViewControllerAnimated:YES];
        }
        if ([strongSelf.delegate respondsToSelector:@selector(searchNearPointWithResult:type:)]) {
            [strongSelf.delegate searchNearPointWithResult:info type:_type];
            [strongSelf.navigationController popViewControllerAnimated:YES];

        }
    }];
    [self setupNavigationBar];
    
    self.resultTableView.estimatedRowHeight = 100;
    self.resultTableView.backgroundColor = kColorBgGray;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getAddress:) name:@"addressNotice" object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
    _type = nil;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"addressNotice" object:nil];
}

- (void)getAddress:(NSNotification *)sender {
    NSString *address = sender.userInfo[@"address"];
    BMKPoiInfo *info = [[BMKPoiInfo alloc] init];
    info.address = address;
    
    if ([_type isEqualToString:@"1"] || [_type isEqualToString:@"2"]) {
        [self.delegate searchNearPointWithResult:info type:_type];
    } else {
        [self.delegate searchNearPointWithResult:info];
    }
    [self.navigationController popViewControllerAnimated:YES];

}

- (void)setupNavigationBar {
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithString:@"#EE7D00"];
    self.navigationItem.titleView = self.searchView;
    UIBarButtonItem * backItme = [[UIBarButtonItem alloc] initWithTitle:self.cityName style:UIBarButtonItemStylePlain target:self action:@selector(clickCityName:)];
    UIBarButtonItem * cancelItem = [[UIBarButtonItem alloc] initWithTitle:@" 取消" style:UIBarButtonItemStylePlain target:self action:@selector(clickCancelItem:)];
    backItme.tintColor = [UIColor whiteColor];
    cancelItem.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = backItme;
    self.navigationItem.rightBarButtonItem = cancelItem;
}

- (void)clickCityName:(UIBarButtonItem *)item {
}

- (void)clickCancelItem:(UIBarButtonItem *)item {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - seter and getter
- (SearchNearPointViewModel *)model {
    if(!_model) {
        _model = [[SearchNearPointViewModel alloc] init];
        _model.tableView = self.resultTableView;
        _model.cityName = self.cityName;
    }
    return _model;
}

- (UITableView *)resultTableView {
    if(!_resultTableView) {
        UITableView * tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        tableview.separatorColor = [UIColor clearColor];
        tableview.tableFooterView = [UIView new];
        tableview.delegate = self.model;
        tableview.dataSource = self.model;
        [self.view addSubview:tableview];
        _resultTableView = tableview;
    }
    return _resultTableView;
}

- (UIView *)searchView {
    if(!_searchView) {
        UIView * search = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width * 0.618f, self.navigationController.navigationBar.frame.size.height * 0.7f)];
        UISearchBar * searchBar = [[UISearchBar alloc] initWithFrame:search.frame];
        [search addSubview:searchBar];
        UIColor * color = [UIColor clearColor];
        searchBar.layer.masksToBounds = YES;
        searchBar.layer.borderWidth = 8;
        search.backgroundColor = color;
        searchBar.backgroundColor = color;
        searchBar.layer.borderColor = [UIColor whiteColor].CGColor;
        searchBar.layer.cornerRadius = 3;
        searchBar.placeholder = @"搜索";
        searchBar.delegate = self.model;
        _searchView = search;
    }
    return _searchView;
}


@end
